package com.androidhelper.module.common.http.exception;

import android.net.ParseException;

import com.androidhelper.module.common.app.LibApplication;
import com.androidhelper.module.common.utils.NetworkUtil;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonSyntaxException;
import com.orhanobut.logger.Logger;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.io.InterruptedIOException;
import java.io.NotSerializableException;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;

import retrofit2.HttpException;

public class ExceptionHandle {
    private static String TAG = "TAG_ExceptionHandle";
    private static String errorMsg = "网络连接异常，请稍后再试";
   // private static int errorCode = ErrorCode.UNKNOWN_ERROR;

    public static String handleException(Throwable e) {
        e.printStackTrace();
        if (e instanceof SocketTimeoutException
                || e instanceof TimeoutException
        ||e instanceof ConnectTimeoutException) {
            Logger.e(TAG, "连接超时：", e.getMessage());
            errorMsg =   "连接超时";
         //   errorCode = ErrorCode.TIMEOUT_ERROR;
        }




        else if(!NetworkUtil.isConnected(LibApplication.getInstance().getApplicationContext())
                ||e instanceof ConnectException
                || e instanceof SocketException){
            Logger.e(TAG, "网络连接失败,请检查网络：", e.getMessage());
            errorMsg =   "网络连接失败,请检查网络";
          //  errorCode = ErrorCode.NETWORK_ERROR;
        }
          else if(e instanceof HttpException){
            HttpException httpException= (HttpException) e;
            errorMsg=convertStatusCode(httpException);
        }



        else if((e instanceof JsonParseException
                     || e instanceof JSONException
                || e instanceof ParseException
                || e instanceof JsonSyntaxException
                || e instanceof JsonSerializer
                || e instanceof NotSerializableException
                )){
            Logger.e(TAG, "解析服务器响应数据失败: " + e.getMessage());
            errorMsg = "解析服务器响应数据失败";
          //  errorCode = ErrorCode.SERVER_ERROR;
        }

        else if (e instanceof ClassCastException) {
            Logger.e(TAG, "类型转换错误: " + e.getMessage());
            errorMsg = "类型转换错误";
        }

        else if (e instanceof javax.net.ssl.SSLHandshakeException) {
            Logger.e(TAG, "证书验证失败: " + e.getMessage());
            errorMsg = "证书验证失败";
        }


        else if(e instanceof RuntimeException){//服务器返回的错误信息
            errorMsg = e.getMessage().toString();
          //  errorCode = ErrorCode.SERVER_ERROR;
        }

        else if(e instanceof UnknownHostException){
            Logger.e(TAG, "不知名主机服务器: " + e.getMessage());
            errorMsg = "不知名主机服务器";
          //  errorCode = ErrorCode.NETWORK_ERROR;
        }


        else if (e instanceof NullPointerException) {
            Logger.e(TAG, "空指针错误: " + e.getMessage());
            errorMsg = "空指针错误";
        }else if (e instanceof OutOfMemoryError) {
            Logger.e(TAG, "内存不足，请清理后台应用: " + e.getMessage());
            errorMsg = "内存不足，请清理后台应用";
        }

        else if (e instanceof InterruptedIOException) {
            Logger.e(TAG, "请求被取消: " + e.getMessage());
            errorMsg = "请求被取消";
        }
        else if(e instanceof IllegalArgumentException){
            errorMsg = "参数错误";
        }else if (e instanceof ServerResponseException) {
            errorMsg=e.getMessage();
        } else{
            errorMsg = "未知错误!";
         }
    return   errorMsg;
    }





    /**
     * 网络异常类型处理类
     * @param httpException
     * @return
     */
    private static String convertStatusCode(HttpException httpException) {
        String msg;
        if(httpException.code()>=500 && httpException.code()<600){
            msg ="服务器处理请求错误";
        }else if(httpException.code()>=400 && httpException.code()<500){
            msg ="服务器无法处理请求";
        }else {
            msg =httpException.message();
        }
        return msg;
    }




}
